package com.ys.otms.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ys.otms.services.IConfigServices;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/config")
public class ConfigController {

    private static Logger logger = LoggerFactory.getLogger(ConfigController.class);

    @Autowired
    private IConfigServices iConfigServices;

    @RequestMapping(value = "/addGoodsConfig",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject addGoodsConfig(HttpServletRequest request) {
        logger.info("ConfigController addGoodsConfig start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iConfigServices.addGoodsConfig(request);
        } catch (Exception e) {
            logger.error("ConfigController addGoodsConfig error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/deleteGoodsConfig",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject deleteGoodsConfig(HttpServletRequest request) {
        logger.info("ConfigController deleteGoodsConfig start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iConfigServices.deleteGoodsConfig(request);
        } catch (Exception e) {
            logger.error("ConfigController deleteGoodsConfig error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/editGoodsConfig",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject editGoodsConfig(HttpServletRequest request) {
        logger.info("ConfigController editGoodsConfig start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iConfigServices.editGoodsConfig(request);
        } catch (Exception e) {
            logger.error("ConfigController editGoodsConfig error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/queryGoodsConfig",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject queryGoodsConfig(HttpServletRequest request) {
        logger.info("ConfigController queryGoodsConfig start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iConfigServices.queryGoodsConfig(request);
        } catch (Exception e) {
            logger.error("ConfigController queryGoodsConfig error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/queryParentGoodsConfig",method = RequestMethod.GET)
    @ResponseBody
    public JSONObject queryParentGoodsConfig(HttpServletRequest request) {
        logger.info("ConfigController queryParentGoodsConfig start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iConfigServices.queryParentGoodsConfig(request);
        } catch (Exception e) {
            logger.error("ConfigController queryParentGoodsConfig error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/queryAllGoodsConfig")
    @ResponseBody
    public String queryAllGoodsConfig(HttpServletRequest request, @RequestParam(value = "page") String page, @RequestParam(value = "limit") String limit) throws Exception {
        logger.info("ConfigController queryAllGoodsConfig start");
        Map<String, Object> result = new HashMap<>();
        try {
            result = iConfigServices.queryAllGoodsConfig(request, page, limit);
        } catch (Exception e) {
            logger.error("ConfigController queryAllGoodsConfig error:", e);
            result.put("SUCCESS",false);
            result.put("MSG","查询错误");
        }
        result.put("code","");
        result.put("msg","");
        return new ObjectMapper().writeValueAsString(result);
    }

    @RequestMapping(value = "/addHuoZhuConfig",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject addHuoZhuConfig(HttpServletRequest request) {
        logger.info("ConfigController addHuoZhuConfig start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iConfigServices.addHuoZhuConfig(request);
        } catch (Exception e) {
            logger.error("ConfigController addHuoZhuConfig error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/deleteHuoZhuConfig",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject deleteHuoZhuConfig(HttpServletRequest request) {
        logger.info("ConfigController deleteHuoZhuConfig start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iConfigServices.deleteHuoZhuConfig(request);
        } catch (Exception e) {
            logger.error("ConfigController deleteHuoZhuConfig error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/editHuoZhuConfig",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject editHuoZhuConfig(HttpServletRequest request) {
        logger.info("ConfigController editHuoZhuConfig start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iConfigServices.editHuoZhuConfig(request);
        } catch (Exception e) {
            logger.error("ConfigController editHuoZhuConfig error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/queryHuoZhuConfig",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject queryHuoZhuConfig(HttpServletRequest request) {
        logger.info("ConfigController queryHuoZhuConfig start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iConfigServices.queryHuoZhuConfig(request);
        } catch (Exception e) {
            logger.error("ConfigController queryHuoZhuConfig error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/queryHuoZhuConfigPage")
    @ResponseBody
    public String queryHuoZhuConfigPage(HttpServletRequest request, @RequestParam(value = "page") String page, @RequestParam(value = "limit") String limit) throws Exception {
        logger.info("ConfigController queryHuoZhuConfig start");
        Map<String, Object> result = new HashMap<>();
        try {
            result = iConfigServices.queryHuoZhuConfigPage(request,page,limit);
        } catch (Exception e) {
            logger.error("ConfigController queryHuoZhuConfig error:", e);
            result.put("SUCCESS",false);
            result.put("MSG","查询错误");
        }
        result.put("code","");
        result.put("msg","");
        return new ObjectMapper().writeValueAsString(result);
    }


    @RequestMapping(value = "/addLogisticsConfig",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject addLogisticsConfig(HttpServletRequest request) {
        logger.info("ConfigController addLogisticsConfig start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iConfigServices.addLogisticsConfig(request);
        } catch (Exception e) {
            logger.error("ConfigController addLogisticsConfig error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/deleteLogisticsConfig",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject deleteLogisticsConfig(HttpServletRequest request) {
        logger.info("ConfigController deleteLogisticsConfig start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iConfigServices.deleteLogisticsConfig(request);
        } catch (Exception e) {
            logger.error("ConfigController deleteLogisticsConfig error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/editLogisticsConfig",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject editLogisticsConfig(HttpServletRequest request) {
        logger.info("ConfigController editLogisticsConfig start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iConfigServices.editLogisticsConfig(request);
        } catch (Exception e) {
            logger.error("ConfigController editLogisticsConfig error:", e);
            dealException(retObj);
        }
        return retObj;
    }


    @RequestMapping(value = "/queryLogisticsConfig",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject queryLogisticsConfig(HttpServletRequest request) {
        logger.info("ConfigController queryLogisticsConfig start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iConfigServices.queryLogisticsConfig(request);
        } catch (Exception e) {
            logger.error("ConfigController queryLogisticsConfig error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/queryLogisticsConfigPage")
    @ResponseBody
    public String queryLogisticsConfigPage(HttpServletRequest request, @RequestParam(value = "page") String page, @RequestParam(value = "limit") String limit) throws Exception {
        logger.info("ConfigController queryLogisticsConfig start");
        Map<String, Object> result = new HashMap<>();
        try {
            result = iConfigServices.queryLogisticsConfigPage(request,page,limit);
        } catch (Exception e) {
            logger.error("ConfigController queryLogisticsConfig error:", e);
            result.put("SUCCESS",false);
            result.put("MSG","查询错误");
        }
        result.put("code","");
        result.put("msg","");
        return new ObjectMapper().writeValueAsString(result);
    }

    @RequestMapping(value = "/queryRole",method = RequestMethod.GET)
    @ResponseBody
    public JSONObject queryRole(HttpServletRequest request) {
        logger.info("ConfigController queryRole start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iConfigServices.queryRole(request);
        } catch (Exception e) {
            logger.error("ConfigController queryRole error:", e);
            dealException(retObj);
        }
        return retObj;
    }


    private void dealException(JSONObject retObj) {
        retObj.put("SUCCESS", false);
        retObj.put("MSG", "system error");
    }


    @RequestMapping("/queryCompanyBAS")
    @ResponseBody
    public JSONObject queryCompanyBAS(HttpServletRequest request) {
        logger.info("ConfigController queryCompanyBAS start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iConfigServices.queryCompanyBAS(request);
        } catch (Exception e) {
            logger.error("ConfigController queryCompanyBAS error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping("/editCompanyBAS")
    @ResponseBody
    public JSONObject editCompanyBAS(HttpServletRequest request) {
        logger.info("ConfigController editCompanyBAS start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iConfigServices.editCompanyBAS(request);
        } catch (Exception e) {
            logger.error("ConfigController editCompanyBAS error:", e);
            dealException(retObj);
        }
        return retObj;
    }

}
